import { cloneDeep, isPlainObject, isString } from "lodash";
import { UIComponents } from "../consts";

export const ArrayOpt = {
	indexOf: ({ data, value, isJson = true, key = "uuid" }) => {
		let dataNew = [];
		if (isJson) {
			data.map(item => {
				dataNew.push(item[key]);
			});
		} else {
			dataNew = data;
		}

		return dataNew.indexOf(value);
	},
	delete: ({ data, value, isJson = true, key = "uuid" }) => {
		const dataNew = cloneDeep(data);
		let index = -1;
		if (isJson) {
			for (let i = 0, len = dataNew.length; i < len; ++i) {
				if (dataNew[i][key] === value) {
					index = i;
					break;
				}
			}
		} else {
			index = dataNew.indexOf(key);
		}
		dataNew.splice(index, 1);
		return dataNew;
	},
	get: ({ data, value, isJson = true, key = "uuid" }) => {
		if (isJson) {
			// console.log(data.length);
			let index = -1;
			for (let i = 0, len = data.length; i < len; ++i) {
				if (data[i][key] === value) {
					index = i;
					break;
				}
			}
			return data[index];
		} else {
			return value;
		}
	},
	toMap: (data, key = "uuid") => {
		const obj = {};

		data.map(item => {
			obj[item[key]] = item;
		});

		return obj;
	}
};

export const JsonOpt = {
	deleteKey: (data, key = "body") => {
		const dataClone = cloneDeep(data);
		delete dataClone[key]; // 删除其子节点数据
		return dataClone;
	}
};

// 返回的是合并的对象
export const PropsOpt = {
	getAll: () => {
		// 所有属性定义
		const allProps = UIComponents.AllPropertyDefine;
		const allPropKeys = Object.keys(allProps);

		return {
			allProps,
			allPropKeys
		};
	},
	getCom: (propsObj, allProps) => {
		// 获取公有属性: 格式 [x, y, z]
		const comProps = UIComponents.ComPropertyDefine();
		comProps.forEach(key => {
			if (isString(key) && key) {
				propsObj[key] = allProps[key];
			} else {
				console.log("key is error: ", key);
			}
		});
	},
	getDefault: (defaultProps, allProps, propsObj) => {
		// default 参数格式为： [x, y, z]
		const defaultObj = {};
		defaultProps.forEach(key => {
			if (isString(key) && key) {
				defaultObj[key] = allProps[key];
			}
		});

		// console.log(defaultProps, allProps, propsObj);

		// 将配置中的default属性和公有属性合并，替换
		Object.assign(propsObj, defaultObj);
	},
	getCustom: (customProps, allProps, propsObj) => {
		customProps.forEach(item => {
			if (isPlainObject(item) && item) {
				const itemClone = cloneDeep(item);

				// 自动生成dataSource default value值
				if (item.property) {
					const dataSource = {};
					const content = item.property.content;
					content &&
						content.map(citem => {
							dataSource[citem.name] = citem.default;

							citem.value &&
								citem.value.map(vItem => {
									vItem.children &&
										vItem.children.map(cvItem => {
											dataSource[cvItem.name] = cvItem.default;
										});
								});
						});
					itemClone["default"] = dataSource;
				}

				Object.assign(propsObj, {
					[item.name]: itemClone
				});

				// console.log(item, propsObj);

				// customObj[item] = allProps[key];
			} else {
				console.log("item is error: ", item);
			}
		});

		// console.log(propsObj);

		// 将配置中的custom自定义属性和上面合并的对象再次合并
		// Object.assign(propsObj, customObj);
	},
	get: (defaultProps, customProps) => {
		const propsObj = {};
		const { allProps } = PropsOpt.getAll();
		PropsOpt.getCom(propsObj, allProps);
		PropsOpt.getDefault(defaultProps, allProps, propsObj);
		PropsOpt.getCustom(customProps, allProps, propsObj);
		return propsObj;
	}
};

export const EditorPageInfoOpt = {
	updateEditorPageItem: (old, data) => {
		const editorPageInfoClone = cloneDeep(old);
		const { uuid, props } = data;

		if (editorPageInfoClone.uuid === uuid) {
			const curProps = editorPageInfoClone.props;
			editorPageInfoClone.props = Object.assign(curProps, props);
		} else {
			EditorPageInfoOpt.loopEditorPageInfo(editorPageInfoClone.body, uuid, props);
		}

		return editorPageInfoClone;
	},

	deleteEditorPageItem: (old, data) => {
		const editorPageInfoClone = cloneDeep(old);
		const deleteItemUuidArr = data;

		if (deleteItemUuidArr.indexOf(editorPageInfoClone.uuid) !== -1) {
			// page页面一般能删除
		} else {
			const editorPageInfoCloneBody = EditorPageInfoOpt.loopDeleteEditorPageInfo(editorPageInfoClone.body, data);
			editorPageInfoClone.body = editorPageInfoCloneBody;
		}

		return editorPageInfoClone;
	},

	/**
	 * 遍历修改数据
	 * @param {[]} data 完整数据
	 * @param {string} uuid key
	 * @param {Object} props 新数据
	 */
	loopEditorPageInfo: (data, uuid, props) => {
		data &&
			data.map(item => {
				if (item.uuid === uuid) {
					const curProps = item.props;
					item.props = Object.assign(curProps, props);
				} else {
					EditorPageInfoOpt.loopEditorPageInfo(item.body, uuid, props);
				}
				return item;
			});
	},
	/**
	 * 遍历删除数据
	 * @param {[]} data 完整数据
	 * @param {string} uuid key
	 * @param {Object} props 新数据
	 */
	loopDeleteEditorPageInfo: (data, uuidArr) => {
		data &&
			data.map((item, index) => {
				// console.log(item, uuidArr);
				if (uuidArr.indexOf(item.uuid) !== -1) {
					data.splice(index, 1, null);
				} else {
					const cData = EditorPageInfoOpt.loopEditorPageInfo(item.body, uuidArr);
					item.body = cData;
				}
				return item;
			});

		return data.filter(item => item !== null);
	},
	/**
	 * 将树形数据转换为扁平的map
	 * @param {{}}} data 树形数据
	 * @param {{}} dataMap data扁平化后的数据 key是uuid
	 */
	toMap: data => {
		const dataMap = {};
		const uuid = data.uuid; // 第一个直接添加
		dataMap[uuid] = JsonOpt.deleteKey(data);

		EditorPageInfoOpt.loopAddEditorPageInfoMap(data.body, dataMap); // 遍历添加

		return dataMap;
	},
	/**
	 *  遍历添加
	 * @param {*} data
	 * @param {*} dataMap
	 */
	loopAddEditorPageInfoMap: (data, dataMap) => {
		data &&
			data.map(item => {
				if (item.body && item.body.length) {
					EditorPageInfoOpt.loopAddEditorPageInfoMap(data.body, dataMap);
				} else {
					dataMap[item.uuid] = JsonOpt.deleteKey(item);
				}
			});
	}
};
